package com.yanqu.road.server.cmd.activity.ghost;

import com.yanqu.road.entity.activity.ghost.GhostBossData;
import com.yanqu.road.entity.activity.ghost.GhostData;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.cross.battle.ghost.GhostActivity;
import com.yanqu.road.server.manager.cross.battle.ghost.GhostMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = CrossProtocol.C2_GHOST_ACTIVITY_GET_CONFIG, desc = "请求鬼市部分配置")
public class CrossGhostGetConfigCmd extends CrossCommand {

    private int activityId;
    private long serverId;
    private long userId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GhostActivityProto.CrossGhostGetBossConfigReqMsg reqMsg = GhostActivityProto.CrossGhostGetBossConfigReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        serverId = reqMsg.getServerId();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        GhostActivity activity = GhostMgr.getActivity(activityId);
        if (activity == null || activity.getGhostData() == null || activity.getGhostConfig() == null) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_OPEN;
            return;
        }

        GhostData ghostData = activity.getGhostData();
        Map<Integer, Map<Integer, GhostBossData>> bossMap = ghostData.getBossMap().get(ghostData.getMaxBatchNo());

        List<Integer> typeList = new ArrayList<>();
        List<Long> hpList = new ArrayList<>();
        for (Map<Integer, GhostBossData> value : bossMap.values()) {
            // 每个类型取一只
            for (GhostBossData bossData : value.values()) {
                typeList.add(bossData.getBossType());
                hpList.add(bossData.getTotalHp());
                break;
            }
        }

        if (typeList.size() > 0) {
            GhostActivityProto.CrossGhostGetBossConfigRespMsg.Builder builder = GhostActivityProto.CrossGhostGetBossConfigRespMsg.newBuilder();
            builder.addAllBossHpList(hpList);
            builder.addAllBossTypeList(typeList);
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(GameProtocol.S_GHOST_GET_CONFIG_FROM_CROSS, builder));
        }
    }

    @Override
    public void sendData() {

    }
}
